import {reqGoodsInfo, reqAddOrUpdateShopCart} from '@/api/index.js'
// 封装模块
import { getUUID } from '@/utils/uuid_token.js'

const state = {
  goodInfo: {},
  // 游客临时身份
  uuid_token: getUUID ()
}
const mutations = {
  GETGOODINFO(state, goodInfo) {
    state.goodInfo = goodInfo
  }
}
const actions = {
  // 获取产品信息的action
  async getGoodInfo (context,skuId) {
    const {data: res}= await reqGoodsInfo(skuId)
    if (res.code == 200) {
      context.commit('GETGOODINFO', res.data)
    }
  },

  // 将产品添加到购物车中
  async addOrUpdateShopCart (context, {skuId, skuNum}) {
    const {data: res} = await reqAddOrUpdateShopCart (skuId, skuNum)
    // 不需要三连环
    if (res.code == 200) {
      // 成功标记
      return 'ok'
    } else {
      // 失败标记
      return Promise.reject (new Error('faile'))
    }
    
  }
}
const getters = {
  // 动态展示数据的时候 简化一下
  categoryView (state) {
     // 这样写有问题  没网的话服务器数据没回来 就是undefined searchList是空对象
    return state.goodInfo.categoryView || {}
  },
  skuInfo (state) {
    return state.goodInfo.skuInfo || {}
  },
  // 简化产品售卖属性
  spuSaleAttrList (state) {
    return state.goodInfo.spuSaleAttrList || []
  }
}

export default {
  state,
  mutations,
  actions,
  getters
}